package com.vxdata.common.utils.逆向工程;

import com.vxdata.common.utils.逆向工程.vo.ReligionReportQvo;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;

/**
 * 创建查询条件的sql
 */
public class CreateQuerySql {

    public static void main(String[] args) {
        // 获取 ReligionReportQvo 类的所有字段
        Field[] fields = ReligionReportQvo.class.getDeclaredFields();
        String qSql = createSql("page", fields, "ReligionReport");
        System.out.println(qSql);
    }

    /**
     * 生成列表查询的 lambda 查询语句
     *
     * @param type      page,list
     * @param fieldList 字段列表
     * @param className 类名
     * @return
     */
    public static String createSql(String type, Field[] fieldList, String className) {

        // 创建一个 StringBuilder 来拼接查询条件
        StringBuilder sql = new StringBuilder();
        if (type.contains("page")) {
            sql.append("Page<" + className + "> dbPage = lambdaQuery()\n");
        } else {
            sql.append("List<" + className + "> dbList = lambdaQuery()\n");
        }

        for (Field f : fieldList) {

            // 获取字段上的 @ApiModelProperty 注解
            ApiModelProperty apiModelProperty = f.getAnnotation(ApiModelProperty.class);
            String notes = apiModelProperty.notes(); // 备注信息

            // 设置字段可访问
            f.setAccessible(true);
            String fieldName = f.getName();
            if (fieldName.equals("pageNum") || fieldName.equals("pageSize")) {
                continue;
            }
            String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

            String fType = f.getType().getSimpleName();
            if (fType.equals("String")) {
                if (notes.contains("like")) {
                    sql.append("    .like(");
                } else {
                    sql.append("    .eq(");
                }
                sql.append("StringUtils.isNotEmpty(qvo." + methodName + "()), " + className + "::" + methodName + ", qvo." + methodName + "())\n");
            } else if (fType.equals("LocalDateTime")) {
                notes = "get" + notes.substring(0, 1).toUpperCase() + notes.substring(1);
                if (fieldName.contains("start")) {
                    sql.append("    .ge(");
                    sql.append("qvo." + methodName + "() != null, " + className + "::" + notes + ", qvo." + methodName + "())\n");

                } else if (fieldName.contains("end")) {
                    sql.append("    .le(");
                    sql.append("qvo." + methodName + "() != null, " + className + "::" + notes + ", qvo." + methodName + "())\n");
                }
            } else {
                sql.append("    .eq(qvo." + methodName + "() != null, " + className + "::" + methodName + ", qvo." + methodName + "())\n");
            }
        }
        // 添加排序条件
        sql.append("    .orderByDesc(" + className + "::getId)\n");
        if (type.contains("page")) {
            sql.append("    .page(new Page<>(qvo.getPageNum(), qvo.getPageSize()));");
        } else {
            sql.append("    .list();");
        }
        return sql.toString();
    }
}
